package com.myMinistry;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.text.DecimalFormat;
import java.util.Locale;

public class Helper {
	// Rounding for the time pickers
    public static int roundMinutes(Integer minute) {
    	if (minute >= 53) // Close to top of the hour
            return 0;
        else if (minute <= 7) // Close to the hour.
            return 0;
        else if (minute <= 23) // Close to quarter after the hour.
            return 15;
        else if (minute <= 37) // Close to half an hour.
            return 30;
    	else
    		return 45;
    }
	
	// Converts minutes into decimal form
	public static String numberFormat(Double number) {
		DecimalFormat format = (DecimalFormat) DecimalFormat.getInstance(Locale.getDefault());
		return format.format(number);
	}
	
	public static String numberFormatNoFloat(double number) {
		return String.valueOf((int) number);
	}
	
	public static void copyFile(FileInputStream fromFile, FileOutputStream toFile) throws IOException {
		FileChannel fromChannel = null;
		FileChannel toChannel = null;
		try {
			fromChannel = fromFile.getChannel();
			toChannel = toFile.getChannel();
			fromChannel.transferTo(0, fromChannel.size(), toChannel);
		} finally {
			try {
				if (fromChannel != null)
					fromChannel.close();
			} finally {
                if (toChannel != null)
                    toChannel.close();
            }
		}
	}
	
	public static String getDayOfMonthSuffix(final int n) {
		if(n >= 1 && n <= 31) {
			if (n >= 11 && n <= 13)
				return "th";
			else {
				switch (n % 10) {
					case 1:  return "st";
					case 2:  return "nd";
					case 3:  return "rd";
					default: return "th";
				}
			}
		}  
	    else
	    	return "illegal day of month: " + n;
	}
}